package com.wenan.data
{
	import com.adobe.serialization.json.JSON;
	import com.wenan.AppGlobal;
	import com.wenan.WenanData;
	import com.wenan.data.Parameter;
	import com.wenan.data.ParameterCollection;
	import com.wenan.data.PositionType;
	import com.wenan.ui.controls.dateChooser.DateUtil;
	import com.wenan.ui.dataProcess.Loading;
	import com.wenan.ui.popup.DialogUtil;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.controls.SWFLoader;
	import mx.core.FlexGlobals;
	import mx.core.IFlexDisplayObject;
	import mx.managers.PopUpManager;
	import mx.messaging.messages.ErrorMessage;
	import mx.messaging.messages.IMessage;
	import mx.rpc.Fault;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class JsonDataLayer implements IDataLayer
	{
		private var appkey:String = "e629f0c72bdd44b7ac9b1bf9419ce70b";
		private var service:HTTPService = new HTTPService();
		private var application:Object;
		private var swfLoader:SWFLoader;
		private var loadSuccess:Boolean = false;
		private var file:FileReference;
		
		public function JsonDataLayer()
		{
			Alert.yesLabel = "确定"; 
			Alert.okLabel = "确定";
			
			file = new FileReference();
			file.addEventListener(IOErrorEvent.IO_ERROR, function(event:IOErrorEvent):void {
				Alert.show("导出失败，请稍后再试！");
			});
			file.addEventListener(Event.COMPLETE, function (event:Event):void {
				Alert.show("导出成功！");			
			});
		}
		
		/**
		 * 获取验证码。周国庆负责。(丁文明，已实现)
		 */
		public function getVerificationCode(resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.commonUrl+"User/VerificationCode.ashx?a=" + Math.random().toString();
			sendCommand(url, dataSignature(new ParameterCollection()), ResultFormat.IMAGE, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, event.result));	
				}
			}
		}
		
		/**
		 * 登陆判断。(丁文明，已实现)
		 */
		public function checkLogin(userName:String, password:String, checkCode:String,
								   resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.commonUrl+"user/CheckLogin.ashx";
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("username", userName));
			parameters.add(new Parameter("userpass", password));
			parameters.add(new Parameter("verificationCode", checkCode));
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				var resultStr:String = String(event.result);
				var result:Object = JSON.decode(resultStr)[0];
				if(result.result == "1")
				{
					if (resultHandler != null)
					{
						resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, result));	
					}
				}else
				{
					if(faultHandler != null)
					{
						var message:ErrorMessage = new ErrorMessage();
						message.faultCode = "1000";
						message.faultDetail = result.msg;
						message.faultString = result.msg;
						
						var fault:Fault = new Fault(message.faultCode, message.faultString, message.faultDetail);
						
						faultHandler(new FaultEvent(FaultEvent.FAULT, false, true, fault, null, message));	
					}
				}
			}
		}
		
		/**
		 * 修改密码。(丁文明，已实现)
		 */
		public function changePassword(userName:String, oldPassword:String, newPassword:String,
									   resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.commonUrl+"user/ChangePassword.ashx";
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("username", userName));
			parameters.add(new Parameter("oldpassword", oldPassword));
			parameters.add(new Parameter("newpassword", newPassword));
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var result:Object = JSON.decode(s)[0];
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, result));	
				}
			}
		}
		
		/**
		 * 获取用户信息。周国庆负责。
		 */
		public function getUserInfo(userId:String, resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.commonUrl+"user/CheckLogin.ashx";
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("userid", userId));
			parameters.add(new Parameter("appkey", appkey));
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var o:Object = JSON.decode(s)[0];
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, o));	
				}
			}
		}
		
		/**
		 * 保存主页。(丁文明，已实现)
		 */
		public function saveHomePageInfo(userId:String,appkey:String,pageInfo:PageInfo, pageInfos:Array,
										 resultHandler:Function = null, 
										 faultHandler:Function = null):void
		{
			var url:String = AppGlobal.commonUrl+"User/SetDefaultPage.ashx";
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("userid", userId));
			parameters.add(new Parameter("appkey", appkey));
			var code:String = toCode(pageInfo);
			if (code == "report_bb" || code == "report_zh" || code == "report_zz" || code == "report_qs" || code == "report_fb")
			{
				parameters.add(new Parameter("pageinfo", pageToJson(pageInfo,pageInfos, "report")));
			} else 
			{
				parameters.add(new Parameter("pageinfo", pageToJson(pageInfo,pageInfos)));
			}
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, commandFaluttHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var o:Object = JSON.decode(s)[0];
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, o));	
				}
			}
			function commandFaluttHandler(event:FaultEvent):void
			{
				Alert.yesLabel = "确定"; 
				Alert.okLabel = "确定";
				
				Alert.show("保存失败");
			}
		}
		
		/**
		 * 获取主页信息。(丁文明，已实现)
		 */
		public function getHomePageInfo(userId:String,appkey:String, resultHandler:Function, 
										faultHandler:Function = null):void
		{
			var url:String = AppGlobal.commonUrl+"User/GetUserInfo.ashx";
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("userid", userId));
			parameters.add(new Parameter("appkey", appkey));
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				var resultStr:String = String(event.result);
				var result:Object = JSON.decode(resultStr)[0];
				if(result.result == "1")
				{
					if (resultHandler != null)
					{
						resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, result));	
					}
				}else
				{
					if(faultHandler != null)
					{
						var message:ErrorMessage = new ErrorMessage();
						message.faultCode = "1000";
						message.faultDetail = result.msg;
						message.faultString = result.msg;
						
						var fault:Fault = new Fault(message.faultCode, message.faultString, message.faultDetail);
						
						faultHandler(new FaultEvent(FaultEvent.FAULT, false, true, fault, null, message));	
					}
				}
			}
		}
		
		/**
		 * 保存浏览页面信息。(丁文明，已实现)
		 */
		public function saveBrowsePageInfo(userId:String,appkey:String, pageInfo:PageInfo, pageInfos:Array,
										   resultHandler:Function = null, 
										   faultHandler:Function = null):void
		{
			var url:String = AppGlobal.commonUrl+"User/SetPageLog.ashx";
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("userid", userId));
			parameters.add(new Parameter("appkey", appkey));
			var code:String = toCode(pageInfo);
			parameters.add(new Parameter("code", code));
			if (code == "report_bb" || code == "report_zh" || code == "report_zz" || code == "report_qs" || code == "report_fb")
			{
				parameters.add(new Parameter("pageinfo", pageToJson(pageInfo,pageInfos, "report")));
			} else 
			{
				parameters.add(new Parameter("pageinfo", pageToJson(pageInfo,pageInfos)));
			}
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var o:Object = JSON.decode(s)[0];
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, o));	
				}
			}
		}
		
		/**
		 * 取得浏览页面信息。(丁文明，已实现)
		 */
		public function getBrowsePageInfos(userId:String, appkey:String, count:int, 
										   resultHandler:Function, 
										   faultHandler:Function = null):void
		{
			var url:String = AppGlobal.commonUrl+"User/GetPageLogs.ashx";
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("userid", userId));
			parameters.add(new Parameter("appkey", appkey));
			parameters.add(new Parameter("count", count.toString()));
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				
				var browsePageInfos:Array = new Array();
				var browsePageInfo:BrowsePageInfo;
				
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var o:Object = JSON.decode(s)[0];
					
					if(o.result == "1")
					{
						var pages:Array = o.pages as Array;
						for(var i=0;i<pages.length;i++)
						{
							browsePageInfo = new BrowsePageInfo();
							browsePageInfo.createData(pages[i]);
							browsePageInfos.push(browsePageInfo);
						}
					}
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, browsePageInfos));	
				}
			}
		}
		
		/**
		 * 用户收藏
		 */
		public function setFavorite(userId:String,appkey:String,pageTitle:String,pageInfo:PageInfo, pageInfos:Array,
											 resultHandler:Function = null, 
											 faultHandler:Function = null):void
		{
			var url:String = AppGlobal.commonUrl+"User/SetFavorite.ashx";
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("userid", userId));
			parameters.add(new Parameter("appkey", appkey));
			parameters.add(new Parameter("pagetitle", pageTitle));
			var code:String = toCode(pageInfo);
			parameters.add(new Parameter("code", code));
			if (code == "report_bb" || code == "report_zh" || code == "report_zz" || code == "report_qs" || code == "report_fb")
			{
				parameters.add(new Parameter("pageinfo", pageToJson(pageInfo,pageInfos, "report")));
			} else 
			{
				parameters.add(new Parameter("pageinfo", pageToJson(pageInfo,pageInfos)));
			}
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				var s:String = String(event.result);
				var o:Object = JSON.decode(s)[0];
				
				if(o.result == "0" && faultHandler != null)
				{
					var message:ErrorMessage = new ErrorMessage();
					message.faultCode = "1000";
					message.faultDetail = o.msg+"，请勿重复收藏";
					message.faultString = o.msg+",请勿重复收藏";
					
					var fault:Fault = new Fault(message.faultCode, message.faultString, message.faultDetail);
					
					faultHandler(new FaultEvent(FaultEvent.FAULT, false, true, fault, null, message));	
					
				}
				else if (o.result == "1" && resultHandler != null)
				{
					var s:String = String(event.result);
					var o:Object = JSON.decode(s)[0];
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, o));	
				}
			}
		}
		
		/**
		 * 获取用户收藏
		 */
		public function getFavorites(userId:String, appkey:String, count:int, 
													resultHandler:Function, 
													faultHandler:Function = null):void
		{
			var url:String = AppGlobal.commonUrl+"User/GetFavorites.ashx";
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("userid", userId));
			parameters.add(new Parameter("appkey", appkey));
			parameters.add(new Parameter("count", count.toString()));
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				
				var browsePageInfos:Array = new Array();
				var browsePageInfo:BrowsePageInfo;
				
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var o:Object = JSON.decode(s)[0];
					
					if(o.result == "1")
					{
						var pages:Array = o.pages as Array;
						for(var i=0;i<pages.length;i++)
						{
							browsePageInfo = new BrowsePageInfo();
							browsePageInfo.createData(pages[i]);
							browsePageInfos.push(browsePageInfo);
						}
					}
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, browsePageInfos));	
				}
			}
		}
		
		/**
		 * 删除用户收藏
		 */
		public function deleteFavorite(userId:String, appkey:String, pageId:String, 
									 resultHandler:Function, 
									 faultHandler:Function = null):void
		{
			var url:String = AppGlobal.commonUrl+"User/DelFavorite.ashx";
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("userid", userId));
			parameters.add(new Parameter("appkey", appkey));
			parameters.add(new Parameter("pageid", pageId));
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, faultResultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if(resultHandler != null)
				{
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, null));	
				}
			}
			
			function faultResultHandler(event:FaultEvent):void
			{
				if(faultHandler != null)
				{
					
				}
			}
		}
		/**
		 * 取得所有省份。(已废弃)
		 */
		public function getProvinceList(resultHandler:Function, faultHandler:Function = null):void
		{
			
		}
		
		/**
		 * 获取全国地图上数据信息
		 */
		public function getMapInfo(resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Report/Country/GetMapInfo.ashx";
			sendCommand(url, dataSignature(new ParameterCollection()), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var o:Object = JSON.decode(s);
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, o));	
				}
			}
		}
		
		public function getCompanyInfo(resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Report/Country/GetCompanyInfo.ashx";
			sendCommand(url, dataSignature(new ParameterCollection()), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var o:Object = new Object();
					o.description=String(event.result);
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, o));	
				}
			}
		}
		
		/**
		 * 取得所有城市。IMPLEMENTED(已废弃)
		 */
		public function getCityList(resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Report/Country/GetMapInfo.ashx";
			sendCommand(url, dataSignature(new ParameterCollection()), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var o:Object = JSON.decode(s);
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, o));	
				}
			}
		}
		
		/**
		 * 取得某个城市的所有广场。IMPLEMENTED  implemented(已废弃)
		 */
		public function getSquareInfoList(cityId:String, resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Report/Country/GetOrgCountByCity.ashx";
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("cityid", cityId));
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var squareInfoList:Array = JSON.decode(s) as Array;
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, squareInfoList));	
				}
			}	
		}
		
		/**
		 * 取得广场楼层信息列表。(陈凯乐)
		 */
		public function getFloorInfoList(squareId:String, resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String=AppGlobal.businessUrl+"Ashx/UPPIT/Report/Org/GetFloorList.ashx";
			var parameters:ParameterCollection=new ParameterCollection();
			parameters.add(new Parameter("orgid",squareId));
			
			sendCommand(url,parameters,ResultFormat.TEXT,commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var floorInfoList:Array = JSON.decode(s) as Array;
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, floorInfoList));	
				}
			}
		}
		
		/**
		 * 获取默认指标接口
		 * @param type 类型，query：查询，real-time：实时，playback：回放
		 * 
		 * */
		public function getIndexData(type:String,resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String=AppGlobal.businessUrl+"Ashx/UPPIT/Report/Floor/GetIndexData.ashx";
			var parameters:ParameterCollection=new ParameterCollection();
			parameters.add(new Parameter("type",type));
			
			sendCommand(url,parameters,ResultFormat.TEXT,commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var o:Object= JSON.decode(s);
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, o));	
				}
			}
		}
		
		/**
		 * 取得某个楼层的店铺信息列表。(已废弃)
		 */
		public function getShopInfoList(squareId:String, floorNo:int, resultHandler:Function, faultHandler:Function = null):void
		{
			var squareList:ArrayCollection = new ArrayCollection([
				{name:"哈尔滨万达"},
				{name:"长春万达"},
				{name:"大连万达"},
				{name:"北京万达"},
				{name:"上海万达"},
				{name:"苏州万达"},
				{name:"南昌万达"},
				{name:"广州万达"},
				{name:"深圳万达"}]);
		}
		
		/**
		 * 取得某个楼层的店铺信息列表。
		 */
		public function getAllShopInfo(dataurl:String,floorid:String, date:String, datetype:String,data:String,
			resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String=AppGlobal.businessUrl+dataurl;
			var parameters:ParameterCollection=new ParameterCollection();
			parameters.add(new Parameter("floorid",floorid));
			parameters.add(new Parameter("date",date));
			parameters.add(new Parameter("datetype",datetype));
			parameters.add(new Parameter("data",data));
			
			sendCommand(url,parameters,ResultFormat.TEXT,commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var o:Object= JSON.decode(s);
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, o));	
				}
			}
		}
		
		/**
		 * 获取资源简介
		 */ 
		public function getResourceInfo(resid:String,isLoading:Boolean,removeLoading:Boolean, resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl + "Ashx/UPPIT/Resource/ResourceInfo.ashx";
			var variables:URLVariables = new URLVariables();
			variables.a =Math.random();
			variables.resid = resid;
			
			
			if (isLoading)
			{
				AppGlobal.beginLoadData();
			}
			
			var request:URLRequest = new URLRequest(url);
			request.method=URLRequestMethod.POST;
			request.data=variables;
			
			var load:URLLoader = new URLLoader() ;
			load.addEventListener(Event.COMPLETE,validateSliceHandler);
			load.load(request) ;
			var flag:Boolean = false;
			function validateSliceHandler(event:Event):void{
				var load:URLLoader=URLLoader(event.target);
				var res:String=load.data.toString();
				
				if(resultHandler != null)
				{
					
					if (removeLoading)
					{
						AppGlobal.endLoadData();
						flag = true;
					}
					
					var result:Object = JSON.decode(res);
					var wendata:WenanData = new WenanData();
					wendata.conver(result, wendata);
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, wendata));	
				}
			}
		}
		
		/**
		 * 取得楼层信息。（陈凯乐，获取某广场的所有楼层信息）
		 */
		public function getFloorInfo(squareId:String, floorNo:int, resultHandler:Function, faultHandler:Function = null):void
		{
		}
		
		/**
		 * 取得店铺排名信息信息(丁文明,已实现)
		 */
		public function getShopRanking(shopId:String,isLoading:Boolean,removeLoading:Boolean, resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String =AppGlobal.businessUrl+"Ashx/UPPIT/Report/Shop/Ranking.ashx";
			
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("shopid",shopId));
			
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, commandFaultHandler,isLoading,removeLoading);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var jsonStr:String = String(event.result);
					var shopInfoData:Object = JSON.decode(jsonStr);
					var targetData:ShopInfo = new ShopInfo();
					targetData.conver(shopInfoData, targetData);
					
					var logoStr:String = targetData.logo;
					if(logoStr != null && logoStr != "")
					{
						var index:int = logoStr.indexOf("Image");
						targetData.prc_path = AppGlobal.businessUrl + targetData.logo.substring(index);
					}
					else
					{
						targetData.prc_path = null;
					}
					
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, targetData));	
				}
			}
			
			function commandFaultHandler(event:FaultEvent):void
			{
				AppGlobal.endLoadData();
				
				if (faultHandler != null)
				{
					faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				}
			}
			
		}
		
		
		/**
		 * 获取主力店列表 (丁文明，未实现)
		 */
		public function getMainShopList(squareId:String,mainShopUnionId:String,resultHandler:Function,faultHandler:Function = null):void
		{
		}
		
		
		/**
		 * 获取集团中主力店商家列表(丁文明，)
		 */
		public function getMainShopUnion(resultHandler:Function,faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Report/MainShop/GetMainShop.ashx";
			
			sendCommand(url, dataSignature(new ParameterCollection()), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var jsonStr:String = String(event.result);
					var mainShopList:Object = JSON.decode(jsonStr) as Array;
					
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, mainShopList));	
				}
			}
		}
		
		/**
		 * 取得店铺多边形列表。
		 * 
		 * @param squareId 广场编号。
		 * @param floorNo 楼层序号。
		 * @param resultHandler 成功的事件处理过程。event.result为ShopPolygon。
		 * @param faultHandler 失败的时间处理过程
		 */
		public function getShopPolygonList(squareId:String, floorNo:int, resultHandler:Function, faultHandler:Function = null):void
		{
			
		}
		/**
		 * 取得集团统计信息（丁文明，已实现）
		 */
		public function getGroupStatisticsInfo(resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Report/Country/GetSummarizeInfo.ashx";
			sendCommand(url, dataSignature(new ParameterCollection()), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				
				if (resultHandler != null)
				{
					var jsonStr:String = String(event.result);
					var statisticsList:Array = JSON.decode(jsonStr) as Array;
					
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, statisticsList));	
				}
			}
		}
		
		/**
		 * 取得广场统计信息。(丁文明，已实现)
		 */
		public function getSquareStatisticsInfo(squareId:String,isLoading:Boolean,removeLoading:Boolean, resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Report/Org/GetSummarizeInfo.ashx";
			
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("orgid",squareId));
			
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, commandFaultHandler,isLoading,removeLoading);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var jsonStr:String = String(event.result);
					var statisticsData:Array = JSON.decode(jsonStr) as Array;
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, statisticsData));	
				}
			}
			
			function commandFaultHandler(event:FaultEvent):void
			{
				AppGlobal.endLoadData();
				
				if (faultHandler != null)
				{
					faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				}
			}
		}
		
		/**
		 * 取得楼层统计信息。
		 */
		public function getFloorStatisticsInfo(squareId:String, floorNo:String, resultHandler:Function, faultHandler:Function = null):void
		{
			
		}
		
		/**
		 * 取得店铺统计信息。（丁文明，已实现）
		 */
		public function getShopStatisticsInfo(shopId:String,isLoading:Boolean,removeLoading:Boolean, resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Report/Shop/GetShopData.ashx";
			
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("shopid",shopId));
			
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, commandFaultHandler,isLoading,removeLoading);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var jsonStr:String = String(event.result);
					var statisticsData:Array = JSON.decode(jsonStr) as Array;
					
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, statisticsData));	
				}
			}
			
			function commandFaultHandler(event:FaultEvent):void
			{
				AppGlobal.endLoadData();
				
				if (faultHandler != null)
				{
					faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				}
			}
			
		}
		
		/**
		 * 取得主力店统计信息。（丁文明）
		 */
		public function getMainShopStatisticsInfo(mainShopId:String,isLoading:Boolean,removeLoading:Boolean, resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Report/MainShop/GetSummarizeInfo.ashx";
			
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("mainshopid",mainShopId));
			
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, commandFaultHandler,isLoading,removeLoading);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var jsonStr:String = String(event.result);
					var statisticsData:Array = JSON.decode(jsonStr) as Array;
					
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, statisticsData));	
				}
			}
			
			function commandFaultHandler(event:FaultEvent):void
			{
				AppGlobal.endLoadData();
				
				if (faultHandler != null)
				{
					faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				}
			}
		}
		
		
		/**
		 * 取得楼层内所有店铺统计信息。
		 * 
		 * @param squareId 广场编号。
		 * @param floorNo 楼层序号。
		 * @param resultHandler 成功的事件处理过程。event.result为ArrayCollection，它的每个项目都是一个ShopStatisticsInfo对象。
		 * @param faultHandler 失败的时间处理过程
		 */
		public function getShopStatisticsInfoList(squareId:String, floorNo:String, resultHandler:Function, faultHandler:Function = null):void
		{
			
		}
		
		
		/**
		 * 取得统计表指标。
		 */
		public function getStatisticsReportIndexList(fileName:String, resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"AdomainUrl2/Index/IndexData.ashx";
			
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("filename",fileName));
			
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, commandFaultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var o:Object = JSON.decode(s);
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, o));	
				}
			}
			
			function commandFaultHandler(event:FaultEvent):void
			{
				if (faultHandler != null)
				{
					faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				}
			}
		}
		
		/**
		 * 取得统计表数据。
		 * 
		 * @param reportType 报表类型。可选值为：表格、柱状图、簇状图、线状图、散点图。
		 * @param filename 文件类型。可选值为：Org、Shop、MainShop。
		 * @param dataColumns 要查询的指标，多个指标用逗号隔开，例curr_day_count，curr_day_sale。若传递空值，则返回所有可选指标。
		 * @param userId 用户编号。当前登录人的用户编号。
		 * @param positionId 所属位置编号。用于过滤数据。
		 * @param positionType 位置类型。本地使用，若要传递给ASHX服务，则需要转换为文件类型。
		 * @param fileId 文件ID串，多个ID中间用逗号隔开，例WAND1001,WAND1002。若传递空值，则返回所有地点（可能是全国、广场、楼层、店铺等）的数据。
		 * @param resultHandler 成功的事件处理过程。event.result为StatisticsReportData。
		 * @param faultHandler 失败的时间处理过程
		 */
		public function getStatisticsReportData(reportType:String, 
												fileName:String, fileIds:String, 
												dataColumns:String, userId:String, 
												positionId:String, positionType:int, 
												fromDate:Date, toDate:Date,
												resultHandler:Function = null, 
												faultHandler:Function = null):void
		{
			var chartData:ArrayCollection = new ArrayCollection();
			
			if (reportType == "表格")
			{
				//				var url:String;
				//				var parameters:ParameterCollection = new ParameterCollection();
				//				
				//				switch(fileName)
				//				{
				//					case "org":
				//						url = "http://wenan2.hust.co/Ashx/UPPIT/Report/Org/GetTableData.ashx";
				//						parameters.add(new Parameter("orgid",dataColumns));//若传递空值，则返回所有广场记录。
				//						parameters.add(new Parameter("data",dataColumns));//若传递空值，则返回所有可选指标。
				//						parameters.add(new Parameter("date",DataUtil.formatDate(fromDate)+","+DataUtil.formatDate(toDate)));
				//						break;
				//					case "shop":
				//						url = "http://wenan2.hust.co/Ashx/UPPIT/Report/Shop/GetTableData.ashx";
				//						parameters.add(new Parameter("shopid",fileIds));//若传递空值，则返回所有店铺记录。
				//						parameters.add(new Parameter("data",dataColumns));//若传递空值，则返回所有可选指标。
				//						parameters.add(new Parameter("date",DataUtil.formatDate(fromDate)+","+DataUtil.formatDate(toDate)));
				//						break;
				//					case "mainshop":
				//						url = "http://wenan2.hust.co/Ashx/UPPIT/Report/MainShop/GetTableData.ashx";
				//						parameters.add(new Parameter("mainshopid",fileIds));//若传递空值，则返回所有主力店记录。
				//						parameters.add(new Parameter("data",dataColumns));//若传递空值，则返回所有可选指标。
				//						parameters.add(new Parameter("date",DataUtil.formatDate(fromDate)+","+DataUtil.formatDate(toDate)));
				//						break;
				//					case "gate":
				//						url = "http://wenan2.hust.co/Ashx/UPPIT/Report/Gate/GetTableData.ashx";
				//						parameters.add(new Parameter("gateid",fileIds));//若传递空值，则返回所有出入口记录。
				//						parameters.add(new Parameter("data",dataColumns));//若传递空值，则返回所有可选指标。
				//						parameters.add(new Parameter("date",DataUtil.formatDate(fromDate)+","+DataUtil.formatDate(toDate)));
				//						break;
				//				}
				//				
				//				sendCommand(url, parameters, ResultFormat.TEXT, commandResultHandler, commandFaultHandler);
				//				
				//				function commandResultHandler(event:ResultEvent):void
				//				{
				//					if (resultHandler != null)
				//					{
				//						var s:String = String(event.result);
				//						var o:Object = JSON.decode(s);
				//						resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, o));	
				//					}
				//				}
				//				
				//				function commandFaultHandler(event:FaultEvent):void
				//				{
				//					if (faultHandler != null)
				//					{
				//						faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				//					}
				//					
				//				}
				
				chartData = new ArrayCollection();
				chartData.addItem({name:"哈尔滨万达", a: 100, b: 22});
				chartData.addItem({name:"长春万达", a: 120, b: 30});
				chartData.addItem({name:"大连万达", a: 90, b: 20});
				chartData.addItem({name:"北京万达", a: 100, b: 26});
				chartData.addItem({name:"上海万达", a: 140, b: 21});
				chartData.addItem({name:"f广场", a: 130, b: 30});
				
				var ySelectableFields:ArrayCollection = new ArrayCollection([
					{title:"本周访问量", propertyName:"a"},
					{title:"当日访问量", propertyName:"b"}
				]);
				var xDefaultField:String = "name";
				var defaultColumnData:ArrayCollection = new ArrayCollection([
					{title:"广场名", propertyName:"name", color:null},
					{title:"本周访问量", propertyName:"a", color:null},
					{title:"当日访问量", propertyName:"b", color:null}
				]);
				var xSelectableFields:ArrayCollection = new ArrayCollection([
					{title:"名称", propertyName:"name", color:0x99FF66}
				]);
				resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, 
					new ReportData(chartData, xSelectableFields, ySelectableFields, xDefaultField, yDefaultFields)));	
			}
			else
			{
				chartData.addItem({year:"2000",name:"哈尔滨万达",a:20,b:5,c:10,d:6});
				chartData.addItem({year:"2001",name:"长春万达",a:21,b:6,c:10,d:10});
				chartData.addItem({year:"2002",name:"大连万达",a:24,b:7,c:9,d:10});
				chartData.addItem({year:"2003",name:"北京万达",a:21,b:5,c:10,d:8});
				chartData.addItem({year:"2004",name:"上海万达",a:24,b:4,c:10,d:8});
				chartData.addItem({year:"2005",name:"f广场",a:26,b:8,c:10,d:9});
				chartData.addItem({year:"2006",name:"g广场",a:27,b:9,c:12,d:9});
				chartData.addItem({year:"2007",name:"h广场",a:29,b:9,c:13,d:8});
				chartData.addItem({year:"2008",name:"s广场",a:30,b:10,c:14,d:7});
				
				var xSelectableFields:ArrayCollection = new ArrayCollection([
					{title:"年份", propertyName:"year"},
					{title:"广场名", propertyName:"name"}
				]);
				var ySelectableFields:ArrayCollection = new ArrayCollection([
					{title:"指标1", propertyName:"a", color:0x99FF66},
					{title:"指标2", propertyName:"b", color:0x0066FF},
					{title:"指标3", propertyName:"c", color:0xFFCC00},
					{title:"指标4", propertyName:"d", color:0xCC99FF}
				]);
				var xDefaultField:String = "year";
				var yDefaultFields:ArrayCollection = new ArrayCollection([
					{title:"指标1", propertyName:"a", color:0x99FF66}
				]);
				resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, 
					new ReportData(chartData, xSelectableFields, ySelectableFields, xDefaultField, yDefaultFields)));	
			}
		}
		
		/**
		 * 获取位置路径。
		 */
		public function getPositionPath(positionId:String, positionType:int,
										resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Nav/GetReportListForType.ashx";
			
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("id",positionId));
			
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, commandFaultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var o:Object = JSON.decode(s);
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, o));	
				}
			}
			
			function commandFaultHandler(event:FaultEvent):void
			{
				if (faultHandler != null)
				{
					faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				}
			}
		}
		
		/**
		 * 获取图表类型数组。
		 */
		public function getStatisticsReportTypeList(pageInfo:PageInfo, 
													resultHandler:Function, 
													faultHandler:Function = null):void
		{
			var statisticsReports:Array = buildStatistics(pageInfo.positionType);
			var commonId:String = pageInfo.parameters.getValue("commonId");
			
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Nav/GetReportListForType.ashx";
			
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("id",commonId?commonId:""));
			
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, commandFaultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					if(statisticsReports.length != 5)return;
					var s:String = String(event.result);
					var resultArr:Array = JSON.decode(s) as Array;
					for(var index:int=0;index<resultArr.length;index++)
					{
						buildStatisticsReports(statisticsReports,resultArr[index]);
					}
					
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, statisticsReports));	
				}
			}
			
			function commandFaultHandler(event:FaultEvent):void
			{
				if (faultHandler != null)
				{
					faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				}
			}
		}
		
		private function buildStatistics(positionType:int):Array
		{
			var statisticsReports:Array = [];
			
			switch(positionType)
			{
				case PositionType.GLOBAL:
					statisticsReports.push({reportName:null,plugurl:null,reportType: "表格", icon:"images/1.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "柱状图", icon:"images/2.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "簇状图", icon:"images/3.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "线状图", icon:"images/4.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "散点图", icon:"images/5.png"});
					break;
				case PositionType.SQUARE:
					statisticsReports.push({reportName:null,plugurl:null,reportType: "表格", icon:"images/1.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "柱状图", icon:"images/2.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "簇状图", icon:"images/3.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "线状图", icon:"images/4.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "散点图", icon:"images/5.png"});
					break;
				case PositionType.SQUARE_NAV:
					statisticsReports.push({reportName:null,plugurl:null,reportType: "表格", icon:"images/1.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "柱状图", icon:"images/2.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "簇状图", icon:"images/3.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "线状图", icon:"images/4.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "散点图", icon:"images/5.png"});
					break;
				case PositionType.FLOOR:
					statisticsReports.push({reportName:null,plugurl:null,reportType: "表格", icon:"images/1.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "柱状图", icon:"images/2.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "簇状图", icon:"images/3.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "线状图", icon:"images/4.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "散点图", icon:"images/5.png"});
					break;
				case PositionType.SHOP:
					statisticsReports.push({reportName:null,plugurl:null,reportType: "表格", icon:"images/1.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "柱状图", icon:"images/2.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "簇状图", icon:"images/3.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "线状图", icon:"images/4.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "散点图", icon:"images/5.png"});
					break;
				case PositionType.MAINSHOP:
					statisticsReports.push({reportName:null,plugurl:null,reportType: "表格", icon:"images/1.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "柱状图", icon:"images/2.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "簇状图", icon:"images/3.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "线状图", icon:"images/4.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "散点图", icon:"images/5.png"});
					break;
				case PositionType.REPORT:
					statisticsReports.push({reportName:null,plugurl:null,reportType: "表格", icon:"images/1.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "柱状图", icon:"images/2.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "簇状图", icon:"images/3.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "线状图", icon:"images/4.png"});
					statisticsReports.push({reportName:null,plugurl:null,reportType: "散点图", icon:"images/5.png"});
					break;
			}
			
			return statisticsReports;
		}
		
		private function buildStatisticsReports(statisticsReports:Array, obj:Object):void
		{
			var reportType:String = obj.type.toString();
			switch(reportType)
			{
				case "表格":
					statisticsReports[0].reportName = obj.name;
					statisticsReports[0].plugurl = obj.plugurl;
					break;
				case "柱状图":
					statisticsReports[1].reportName = obj.name;
					statisticsReports[1].plugurl = obj.plugurl;
					break;
				case "簇状图":
					statisticsReports[2].reportName = obj.name;
					statisticsReports[2].plugurl = obj.plugurl;
					break;
				case "线状图":
					statisticsReports[3].reportName = obj.name;
					statisticsReports[3].plugurl = obj.plugurl;
					break;
				case "散点图":
					statisticsReports[4].reportName = obj.name;
					statisticsReports[4].plugurl = obj.plugurl;
					break;
			}
		}
		
		/**
		 * 排名明细。
		 */
		public function getReportListForStandard(resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Nav/GetReportListForStandard.ashx";
			
			sendCommand(url, dataSignature(new ParameterCollection()), ResultFormat.TEXT, commandResultHandler, faultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				var resultArr:Array;
				
				if(resultHandler != null)
				{
					resultArr = JSON.decode(event.result.toString());
					
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, resultArr));	
				}
			}
			
		}
		
		/**
		 * 排行榜
		 * */
		public function getTopPositionList(id:String, resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Nav/GetReportListForRanking.ashx";
			
			var parameters:ParameterCollection = new ParameterCollection();
			if (!id)
			{
				id = "";
			}
			parameters.add(new Parameter("id",id));
			
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, commandFaultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var arr:Array = JSON.decode(s) as Array;
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, new ArrayCollection(arr)));	
				}
			}
			
			function commandFaultHandler(event:FaultEvent):void
			{
				if (faultHandler != null)
				{
					faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				}
			}
		}
		
		/**
		 * 多维导航
		 * */
		public function getNavigationList(id:String, resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Nav/MutiNav.ashx?a=" + Math.random().toString();
			
			var parameters:ParameterCollection = new ParameterCollection();
			if (!id)
			{
				id = "";
			}
			parameters.add(new Parameter("id",id));
			
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, commandFaultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var arr:Array = JSON.decode(s) as Array;
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, new ArrayCollection(arr)));	
				}
			}
			
			function commandFaultHandler(event:FaultEvent):void
			{
				if (faultHandler != null)
				{
					faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				}
			}
		}
		
		/**
		 * 获取常用报表
		 * */
		public function getCommonlyUsedReportList(id:String, resultHandler:Function, faultHandler:Function = null):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Nav/GetReportListForCommon.ashx";
			
			var parameters:ParameterCollection = new ParameterCollection();
			if (!id)
			{
				id = "";
			}
			parameters.add(new Parameter("id",id));
			
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, commandFaultHandler);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var arr:Array = JSON.decode(s) as Array;
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, new ArrayCollection(arr)));	
				}
			}
			
			function commandFaultHandler(event:FaultEvent):void
			{
				if (faultHandler != null)
				{
					faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				}
			}
		}
		
		/**
		 * 获取默认报表选项数据
		 * */
		public function getDefaultReportItemData(name:String, resultHandler:Function, faultHandler:Function = null, isLoading:Boolean = true, removeLoading:Boolean = true):void
		{
			var url:String = AppGlobal.businessUrl+"Ashx/UPPIT/Report/GetReportData.ashx";
			
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("name", escape(name)));
			
			sendCommand(url, dataSignature(parameters), ResultFormat.TEXT, commandResultHandler, commandFaultHandler, isLoading, removeLoading);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					var obj:Object = JSON.decode(s) as Object;
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, obj));	
				}
			}
			
			function commandFaultHandler(event:FaultEvent):void
			{
				if (faultHandler != null)
				{
					faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				}
			}
		}
		
		/**
		 * 查询报表数据
		 * @param url  访问的地址
		 * @param resname 文件类型  可选值为：Org、Shop、MainShop。
		 * @param ids 文件ID串，多个ID中间用逗号隔开，例WAND1001,WAND1002。若传递空值，则返回所有地点（可能是全国、广场、楼层、店铺等）的数据。
		 * @param columns 要查询的指标，多个指标用逗号隔开，例curr_day_count，curr_day_sale。若传递空值，则返回所有可选指标。
		 * */
		public function queryReportData(url:String, resname:String, date:String, datetype:String, ids:String, columns:String, charttype:String, resultHandler:Function, faultHandler:Function = null, isLoading:Boolean = true, removeLoading:Boolean = true):void
		{
			var reqUrl:String = AppGlobal.businessUrl + url + "?a=" + Math.random().toString();
			
			var parameters:ParameterCollection = new ParameterCollection();
			parameters.add(new Parameter("a", Math.random().toString()));
			parameters.add(new Parameter("date", date));
			parameters.add(new Parameter("datetype", datetype));
			if (resname == "广场名称")
			{
				parameters.add(new Parameter("orgid", ids));
			} else if (resname == "店铺名称")
			{
				parameters.add(new Parameter("shopid", ids));
			} else if (resname == "主力店名称")
			{
				parameters.add(new Parameter("mainshopid", ids));
			} else if (resname == "出入口名称")
			{
				parameters.add(new Parameter("gateid", ids));
			}
			parameters.add(new Parameter("data", StringUtil.trimRight(columns)));
			parameters.add(new Parameter("charttype", charttype));
			sendCommand(reqUrl, parameters, ResultFormat.TEXT, commandResultHandler, commandFaultHandler, isLoading, removeLoading);
			
			function commandResultHandler(event:ResultEvent):void
			{
				if (resultHandler != null)
				{
					var s:String = String(event.result);
					if (s && s != "")
					{
						var obj:Object = JSON.decode(s) as Object;
						resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, obj));	
					}
				}
			}
			
			function commandFaultHandler(event:FaultEvent):void
			{
				if (faultHandler != null)
				{
					faultHandler(new FaultEvent(FaultEvent.FAULT,false,true));
				}
			}
		}
		
		/**
		 * 导出报表
		 * */
		public function exportDataToExcel():void
		{
			var reqUrl:String = AppGlobal.businessUrl + "Ashx/UPPIT/Report/ExpDataToExcel.ashx?a=" + Math.random().toString();
			var request:URLRequest = new URLRequest(reqUrl);
			file.download(request, DateUtil.getDateString() + ".xls");
		}
		
		/**
		 * 获取界面切换按钮数据。
		 */
		public function getChangeButton(pageInfo:PageInfo, 
										resultHandler:Function, 
										faultHandler:Function = null):void
		{
			var resultPageInfo:PageInfo = null;
			var parameters:ParameterCollection = pageInfo.parameters;
			var positionType:int = pageInfo.positionType;
			
			switch(positionType)
			{
				case PositionType.GLOBAL:
					if(parameters.findByName("styleName"))
					{
						parameters.findByName("styleName").value = "ChangeBrandButton";
					}else
					{
						parameters.add(new Parameter("styleName","ChangeBrandButton"));
					}
					resultPageInfo = pageInfo;
					break;
			}
			resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, resultPageInfo));
		}
		
		/**
		 * 发送命令。
		 */
		private function sendCommand(url:String, parameters:ParameterCollection, resultFormat:int,
									 resultHandler:Function = null, faultHandler:Function = null, isLoading:Boolean = true, removeLoading:Boolean = true):void
		{
			if (isLoading)
			{
				AppGlobal.beginLoadData();
			}
			switch(resultFormat)
			{
				case ResultFormat.TEXT:
					requestText(url, parameters, resultHandler, faultHandler, removeLoading);
					break;
				case ResultFormat.IMAGE:
					requestImage(url, parameters, resultHandler, faultHandler, removeLoading);
					break;
			}
		}
		
		private function requestText(url:String, parameters:ParameterCollection, 
									 resultHandler:Function = null, faultHandler:Function = null, removeLoading:Boolean = true):void
		{
			var service:HTTPService = new HTTPService();
			service.url = url;
			service.useProxy = false;
			service.method = "post";
			service.resultFormat="text";
			var flag:Boolean = false;
			if (resultHandler != null)
			{
				if (removeLoading)
				{
					AppGlobal.endLoadData();
					flag = true;
				}
				service.addEventListener(ResultEvent.RESULT, resultHandler);	
			}
			if (faultHandler != null)
			{
				if (removeLoading && !flag)
				{
					AppGlobal.endLoadData();
				}
				service.addEventListener(FaultEvent.FAULT, faultHandler);
			}
			service.send(getURLVariables(parameters));
		}
		
		
		private function requestImage(url:String, parameters:ParameterCollection,
									  resultHandler:Function = null, faultHandler:Function = null, removeLoading:Boolean = true):void
		{
			var loader:Loader = new Loader();			
			var reuqest:URLRequest = new URLRequest(url);
			reuqest.data = getURLVariables(parameters); 
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadImageComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoadImageFault);
			loader.load(reuqest);
			var flag:Boolean = false;
			function onLoadImageComplete(event:Event):void
			{
				if (resultHandler != null)
				{
					if (loadSuccess && removeLoading)
					{
						try {
							application.removeElement(swfLoader);
						} catch (e:Error){}
						flag = true;
					}
					var bitmap:Bitmap=Bitmap(loader.content);
					resultHandler(new ResultEvent(ResultEvent.RESULT, false, true, bitmap));	
				}
			}
			
			function onLoadImageFault(event:IOErrorEvent):void
			{
				if (faultHandler != null)
				{
					if (loadSuccess && removeLoading && !flag)
					{
						try {
							application.removeElement(swfLoader);
						} catch (e:Error){}
					}
					
					var message:ErrorMessage = new ErrorMessage();
					message.faultCode = "1001";
					message.faultDetail = event.text;
					message.faultString = event.text;
					
					var fault:Fault = new Fault(message.faultCode, message.faultString, message.faultDetail);
					
					faultHandler(new FaultEvent(FaultEvent.FAULT, false, true, fault, null, message));	
				}
			}
		}
		
		/**
		 * 获取HTTPService参数。
		 */
		private function getURLVariables(parameters:ParameterCollection):URLVariables
		{
			if (parameters && parameters.length > 0)
			{
				var dest:URLVariables = new URLVariables();
				var parameter:Parameter;
				for(var i:int=0;i<parameters.length;i++)
				{
					parameter = parameters.getAt(i);
					dest[parameter.name] = parameter.value;
				}
				return dest;	
			}
			return null;
		}
		
		
		private function pageToJson(pageInfo:PageInfo,pageInfos:Array, type:String = null):String
		{
			var pageInfoStr:String = pageInfo.pageInfoToJson(type);
			var pageInfosStr:String = pageInfosToJson(pageInfos);
			return pageInfoStr.substring(0,pageInfoStr.lastIndexOf("}")) + ',"pageInfos":' + pageInfosStr+ "}";
		}
		
		private function pageInfosToJson(pageInfos:Array):String
		{
			var pageInfosStr:String = '[';
			var pageInfo:PageInfo;
			var pageName:String;
			for(var i:int=0;i<pageInfos.length;i++)
			{
				pageInfo = (pageInfos[i] as PageInfo);
				pageName = pageInfo.pageName;
				if(i == pageInfos.length-1)
				{
					pageInfosStr += pageInfo.pageInfoToJson(pageName == "report" ? "report":"") ;
				}else
				{
					pageInfosStr += pageInfo.pageInfoToJson(pageName == "report" ? "report":"");
					pageInfosStr += ",";
				}
			}
			
			pageInfosStr += "]";
			return pageInfosStr;
		}
		
		private function toCode(pageInfo:PageInfo):String
		{
			var resultCode:String = null;
			var pageName:String = pageInfo.pageName;
			switch(pageName)
			{
				case "CountryHomePage":
					resultCode = "country";
					break;
				case "SquareHomePage":
					resultCode = "org_home";
					break;
				case "ShopHomePage":
					resultCode = "shop_home";
					break;
				case "MainShopHomePage":
					resultCode = "brand_home";
					break;
				case "CountryNavigator":
					resultCode = "country";
					break;
				case "SquareNavigator":
					resultCode = "org_navigation";
					break;
				case "FloorNavigator":
					resultCode = "floor";
					break;
				case "Report":
					resultCode = "report_bb";
					break;
				case "UpdatePage":
					resultCode = "updatepassword_home";
					break;
				
			}
			
			if(resultCode == "report_bb")
			{
				var reportType:String = pageInfo.parameters.findByName("ReportType").value;
				switch(reportType)
				{
					case "表格":
						resultCode = "report_bb";
						break;
					case "柱状图":
						resultCode = "report_zh";
						break;
					case "簇状图":
						resultCode = "report_zz";
						break;
					case "线状图":
						resultCode = "report_qs";
						break;
					case "散点图":
						resultCode = "report_fb";
						break;
				}
			}
			
			return resultCode;
		}
		
		
		/**
		 * 数据签名组装数据
		 */
		private function dataSignature(parameters:ParameterCollection):ParameterCollection
		{
			var md5:MD5 = new MD5();
			var sigValue:String = "";
			if (parameters && parameters.length > 0)
			{
				var parameter:Parameter;
				for(var i:int=0;i<parameters.length;i++)
				{
					parameter = parameters.getAt(i);
					sigValue += parameter.name + "=" + parameter.value;
				}
			}
			//拼接时间戳
			sigValue += "timestamp" + "=" + (new Date()).time;
			//私钥
			sigValue += "SecretKey" + "=" + AppGlobal.UUID;
			parameters.add(new Parameter("signature", md5.getMD5(sigValue)));
			return parameters;
		}
	}
}